package football.core;

import playn.core.CanvasImage;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.PlayN;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author tol
 */
public class SpriteImages {
    public static class SpriteData{
        public final CanvasImage image;
        public final int originX;
        public final int originY;
        public SpriteData(Image spriteImage, int x, int y, int w, int h, int ox, int oy){
            image= PlayN.graphics().createImage(w, h);
            image.canvas().drawImage(spriteImage, 0, 0, w, h, x, y, w, h);
            originX=ox;
            originY=oy;
        }
        public SpriteData(Image spriteImage, int x, int y, int w, int h){
            this(spriteImage, x, y, w, h, 0, 0);
        }
        public SpriteData(int color, int w, int h){
            image = PlayN.graphics().createImage(w, h);
            image.canvas().setFillColor(color);
            image.canvas().fillRect(0,0,w, h);
            originX=0;
            originY=0;
        }
    }

    private static Map<Sprites,SpriteData> map=new HashMap<Sprites,SpriteData>();
    public static final int cellW = 15;
    public static final int cellH = 15;



    public static void setSpriteImage(Image image){
        map.put(Sprites.Actor,    new SpriteData(image,0,0,14,14));
        map.put(Sprites.Guard,    new SpriteData(image,30,0,15,15));
        map.put(Sprites.Camera,   new SpriteData(image,60,0,15,15));
        map.put(Sprites.Start,    new SpriteData(image,0,30,15,15));
        map.put(Sprites.End,      new SpriteData(image,30,30,15,15));
        map.put(Sprites.Action,   new SpriteData(image,60,30,15,15));
        map.put(Sprites.Space,    new SpriteData(image,0,60,15,15));
        map.put(Sprites.Road,     new SpriteData(image,30,60,15,15));
        map.put(Sprites.Door,     new SpriteData(image,60,60,15,15));
        map.put(Sprites.CamView,  new SpriteData(image,90,0,118,118));
        map.put(Sprites.CamViewTop,  new SpriteData(image,143,0,15,60));
        map.put(Sprites.CamViewBottom,  new SpriteData(image,143,60,15,60));
        map.put(Sprites.CamViewLeft,  new SpriteData(image,90,53,60,15));
        map.put(Sprites.CamViewRight,  new SpriteData(image,150,53,60,15));
        map.put(Sprites.SoundOn,  new SpriteData(image,0,90,30,30));
        map.put(Sprites.SoundOff,  new SpriteData(image,30,90,30,30));
        map.put(Sprites.PauseOn,  new SpriteData(image,225,0,30,30));
        map.put(Sprites.PauseOff,  new SpriteData(image,225,30,30,30));
    }


    public static SpriteData get(Sprites sprite){
        SpriteData sd=map.get(sprite);
        if(sd==null)
            sd=new SpriteData(0x77777777,60,60);
        return sd;
    }

    public static ImageLayer toLayer(Sprites sprite, ImageLayer layer){
        SpriteData sd=get(sprite);
        layer.setImage(sd.image);
        layer.setOrigin(sd.originX, sd.originY);
        return layer;
    }



    public static ImageLayer createLayer(Sprites sprite){

        SpriteData sd=get(sprite);
        ImageLayer layer = PlayN.graphics().createImageLayer(sd.image);

        layer.setOrigin(sd.originX, sd.originY);
        return layer;
    }
}
